package pack0902;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TongXinJianLi {
    public static void main(String[] args) throws Exception{
        //建立服务器
        ServerSocket ss = new ServerSocket(9989);
        System.out.println("服务器建立了");
        //建立服务端接字
        System.out.println("等待客户端连接");
        Socket socket = ss.accept();
        System.out.println("客户端已连接");

        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();

        Txjl1 txjl1 = new Txjl1(os);
        Txjl2 txjl2 = new Txjl2(is);
        txjl1.start();
        txjl2.start();
    }
}
class Txjl1 extends Thread{
    OutputStream os;

    public Txjl1(OutputStream os) {
        this.os = os;
    }

    public void run() {
        Scanner in = new Scanner(System.in);
        try {
            while (true){
                String str = in.next();
                if ("exit".equals(str)){
                    break;
                }
                os.write(str.getBytes());
                Thread.sleep(1000);
            }
            os.close();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
class Txjl2 extends Thread{
    InputStream is;

    public Txjl2(InputStream is) {
        this.is = is;
    }

    public void run() {
        try {
            byte[] bytes = new byte[100];
            while (true){
                int len = is.read(bytes);
                String str = new String(bytes,0,len);
                System.out.println(str);
                Thread.sleep(1000);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

